<template>
  <el-button type="primary" @click="toExport">导出报表</el-button>
</template>
<script>
import * as XLSX from 'xlsx'
import { saveAs } from 'file-saver'
export default {
  props: {
    list: {
      type: Array,
      default() {
        return []
      }
    },
    name: {
      type: String,
      default: '导出数据'
    }
  },
  methods: {
    toExport() {
      if (!this.list.length) {
        this.$message.warning('数据为空')
        return
      }
      // 假设你的表格数据是从data属性中的一个数组获取的
      const data = this.list.map(row => Object.values(row))
      const worksheet = XLSX.utils.aoa_to_sheet(data)
      const workbook = XLSX.utils.book_new()
      XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1')
      const excelBuffer = XLSX.write(workbook, {
        bookType: 'xlsx',
        type: 'array'
      })
      const dataBlob = new Blob([excelBuffer], {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
      })
      saveAs(dataBlob, this.name + '.xlsx')
    }
  }
}
</script>
